const TrafficService = require('../services/trafficService')

function autoBind(instance) {
  const proto = Object.getPrototypeOf(instance)
  const methodNames = Object.getOwnPropertyNames(proto).filter((key) => typeof instance[key] === 'function' && key !== 'constructor')

  for (const key of methodNames) {
    instance[key] = instance[key].bind(instance)
  }
}
class TrafficController {
  constructor() {
    autoBind(this)
  }

  // 通用请求处理函数
  async handleRequest(req, res, serviceMethod, params = [], successMsg) {
    try {
      const data = await serviceMethod(...params)
      res.json({
        code: 200,
        data,
        msg: successMsg || '获取成功',
      })
    } catch (error) {
      res.json({
        code: 500,
        msg: successMsg ? successMsg.replace('成功', '失败') : '操作失败',
        error: error?.message,
      })
    }
  }

  // 获取商家详情
  async getlist1(req, res) {
    return this.handleRequest(req, res, TrafficService.getlist1, [])
  }

  async getlsitMy(req, res) {
    return this.handleRequest(req, res, TrafficService.getlsitMy, [])
  }

  async getRealTimeData(req, res) {
    return this.handleRequest(req, res, TrafficService.getRealTimeData, [])
  }

  async getHistoryList(req, res) {
    return this.handleRequest(req, res, TrafficService.getHistoryList, [])
  }

  async getAlertList(req, res) {
    return this.handleRequest(req, res, TrafficService.getAlertList, [])
  }

  async getPointsData(req, res) {
    return this.handleRequest(req, res, TrafficService.getPointsData, [])
  }

  async getPannelData(req, res) {
    return this.handleRequest(req, res, TrafficService.getPannelData, [])
  }

  // 用户管理相关控制器方法
  async getUserList(req, res) {
    return this.handleRequest(req, res, TrafficService.getUserList, [req.query], '获取用户列表成功')
  }

  async getUserDetail(req, res) {
    const { id } = req.params
    return this.handleRequest(req, res, TrafficService.getUserDetail, [id], '获取用户详情成功')
  }

  async addUser(req, res) {
    return this.handleRequest(req, res, TrafficService.addUser, [req.body], '添加用户成功')
  }

  async updateUser(req, res) {
    return this.handleRequest(req, res, TrafficService.updateUser, [req.body], '更新用户成功')
  }

  async updateUserStatus(req, res) {
    const { id } = req.params
    const { status } = req.body
    return this.handleRequest(req, res, TrafficService.updateUserStatus, [id, status], '更新用户状态成功')
  }

  async deleteUser(req, res) {
    const { id } = req.params
    return this.handleRequest(req, res, TrafficService.deleteUser, [id], '删除用户成功')
  }

  async batchUpdateUserStatus(req, res) {
    const { ids, status } = req.body
    return this.handleRequest(req, res, TrafficService.batchUpdateUserStatus, [ids, status], '批量更新用户状态成功')
  }

  async login(req, res) {
    return this.handleRequest(req, res, TrafficService.login, [req.body],'登录成功')
  }

  async anyPost(req, res) {
    res.json({
      code: 200,
      data: true,
      msg: '获取成功',
    })
  }
  async anyGet(req, res) {
    res.json({
      code: 200,
      data: true,
      msg: '获取成功',
    })
  }
}

module.exports = new TrafficController()
